package com.hongxun.project.assets.pMaterial.controller;

import java.util.List;
import java.util.Map;

import com.hongxun.common.utils.text.Convert;
import com.hongxun.project.assets.iMaterial.service.IIMaterialService;
import com.hongxun.project.assets.pBoxes.domain.PBoxes;
import com.hongxun.project.assets.pBoxes.service.IPBoxesService;
import com.hongxun.project.assets.pPoint.domain.PPoint;
import com.hongxun.project.assets.pPoint.service.IPPointService;
import com.hongxun.project.assets.pTask.domain.PTask;
import com.hongxun.project.assets.pTask.service.IPTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.pMaterial.domain.PMaterial;
import com.hongxun.project.assets.pMaterial.service.IPMaterialService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

/**
 * 配电材料采集 信息操作处理
 * 
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/pMaterial")
public class PMaterialController extends BaseController
{
    private String prefix = "assets/pMaterial";
	
	@Autowired
	private IPMaterialService pMaterialService;
	@Autowired
	private IPBoxesService pBoxesService;
	@Autowired
	private IPPointService pPointService;
	@Autowired
	private IPTaskService pTaskService;
	@Autowired
	private IIMaterialService iMaterialService;


	
//	@RequiresPermissions("assets:pMaterial:view")
	@GetMapping()
	public String pMaterial(PMaterial pMaterial , ModelMap modelMap)
	{
		String pointId = pMaterial.getPointId();

		PTask pTask = pTaskService.selectPTaskById(pMaterial.getTaskId());
		PPoint pPoint = pPointService.selectPPointByIdForState(pointId,pMaterial.getTaskId());
		modelMap.put("lineId",pPoint.getLineId());	// 线路ID
		modelMap.put("byqId",pPoint.getByqId());	// 变压器ID
		modelMap.put("taskId",pPoint.getTaskId());	// 任务ID
		modelMap.put("pointId",pointId);			// 点位ID
		modelMap.put("taskStatus", pTask.getTaskStatus());// 任务状态

	    return prefix + "/pMaterial";
	}

	/*@GetMapping("/task_pMaterial/{taskId}")
	public String task_pMaterial(@PathVariable("taskId") String taskId , ModelMap modelMap)
	{
		modelMap.put("taskId",taskId);
	    return prefix + "/task-pMaterial";
	}*/
	
	/**
	 * 查询配电材料采集列表
	 */
//	@RequiresPermissions("assets:pMaterial:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PMaterial pMaterial)
	{
		startPage();
        List<PMaterial> list = pMaterialService.selectPMaterialList(pMaterial);
		return getDataTable(list);
	}

	
	/**
	 * 导出配电材料采集列表
	 */
//	@RequiresPermissions("assets:pMaterial:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PMaterial pMaterial)
    {
    	List<PMaterial> list = pMaterialService.selectPMaterialList(pMaterial);
        ExcelUtil<PMaterial> util = new ExcelUtil<PMaterial>(PMaterial.class);
        return util.exportExcel(list, "pMaterial");
    }
	
	/**
	 * 新增配电材料采集
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存配电材料采集
	 */
//	@RequiresPermissions("assets:pMaterial:add")
	@Log(title = "配电材料采集", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PMaterial pMaterial)
	{
		pMaterial.setDatastate("新增");
		return toAjax(pMaterialService.insertPMaterial(pMaterial));
	}

	/**
	 * 修改配电材料采集
	 */
	@GetMapping("/edit/{id}/{taskId}")
	public String edit(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap)
	{
		PMaterial pMaterial = pMaterialService.selectPMaterialByIdForState(id,taskId);
		mmap.put("pMaterial", pMaterial);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存配电材料采集
	 */
//	@RequiresPermissions("assets:pMaterial:edit")
	@Log(title = "配电材料采集", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PMaterial pMaterial)
	{
		pMaterial.setDatastate("修改");
		return toAjax(pMaterialService.updatePMaterial(pMaterial));
	}
	
	/**
	 * 删除配电材料采集
	 */
//	@RequiresPermissions("assets:pMaterial:remove")
	@Log(title = "配电材料采集", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(pMaterialService.deletePMaterialByIds(ids));
	}


	/**
	 * 删除配电材料采集
	 */
//	@RequiresPermissions("assets:pMaterial:remove")
	@Log(title = "配电材料采集", businessType = BusinessType.DELETE)
	@PostMapping( "/remove2Id")
	@ResponseBody
	public AjaxResult remove2Id(String ids , String taskIds)
	{

		String[] str = Convert.toStrArray(ids);
		String[] strTa = Convert.toStrArray(taskIds);
		int i = 0;
		for (String s : str) {
			PMaterial pMaterial = new PMaterial();
			pMaterial.setId(s);
			pMaterial.setDatastate("删除");
			for (String s1 : strTa) {
				pMaterial.setTaskId(s1);
				i = pMaterialService.updatePMaterial(pMaterial);
			}
			int ds = iMaterialService.deleteIMaterialByIds(s);
		}
		return toAjax(i);

//		return toAjax(pMaterialService.deletePMaterialByIdsAndTaskIds(ids , taskIds));
	}

}
